package com.whz.annontateddemo;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: sinosoft
 * @Description:
 * @Date: Create in 16:19 2021/5/17
 * @Modified By:
 */
@Slf4j
public class RunTests2 {
    public static void main(String[] args) throws ClassNotFoundException {
        int tests = 0;
        int passed = 0;
        Class<?> clazz = Class.forName("com.whz.annontateddemo.Sample2");
        for (Method m : clazz.getDeclaredMethods()) {
            tests ++;
            try {
                m.invoke(null);
                log.info("方法: {} 失败没有异常抛出", m);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException wrappedExc) {
                Throwable exc = wrappedExc.getCause();
                Class<? extends Throwable> excType = m.getAnnotation(ExceptionTest.class).value();
                if(excType.isInstance(exc)) {
                    passed ++;
                } else {
                    log.info("测试未通过 {} 方法抛出的异常是 {} 要求的异常是 {}", m, exc, excType.getName());
                }
            } catch (Exception exc) {
                log.error("不合法的 @Test 方法: {}", m);
            }

        }
        log.info("共 {} 个方法抛出指定异常", passed);
    }
}
